/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.adextensions_service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.hiddenequity.acts.Act_Super;
import com.hiddenequity.utils.Converter;
import com.hiddenequity.utils.XMLUtils;

public class CampaignAdExtensionResponseParser {

	Document _XMLDocument;


	/*
	 * Constructor
	 */
	public CampaignAdExtensionResponseParser(String XMLData){
		try {
			_XMLDocument = XMLUtils.XMLStringToDoc(XMLData);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}//Constructors
	
	
	
	/*
	 * Check response for errors
	 */
	public String checkForErrors(){
		String error = null;
		try {
	        
	        NodeList faultstring = _XMLDocument.getElementsByTagName("faultstring");
	        error =  XMLUtils.getCharacterDataFromElement((Element) faultstring.item(0));
	        
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
		return error;
	}//checkForErrors
	
	
	/*
	 * Parse location extension list
	 */
	public ArrayList<Map<String, String>> getLocationExtensionList(){

		ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
		
		try {
	        
			Act_Super.resetTotals();
			
			NodeList nodes = _XMLDocument.getElementsByTagName("entries");
			
			int count = nodes.getLength();
			
	        for (int i = 0; i < count; i++) {
                //fill up hush map for list population
            	Map<String, String> temp = new HashMap<String, String>();

	        	Element element = (Element) nodes.item(i);

	        	NodeList adExtensionType = element.getElementsByTagName("AdExtension.Type");
	        	if(!XMLUtils.getCharacterDataFromElement((Element) adExtensionType.item(0)).equals("LocationExtension"))
	        		continue;
	        	
	        	NodeList id = element.getElementsByTagName("id");
	        	temp.put("id", XMLUtils.getCharacterDataFromElement((Element) id.item(0)));
	        	
	        	NodeList campaignId = element.getElementsByTagName("campaignId");
	        	temp.put("campaignId", XMLUtils.getCharacterDataFromElement((Element) campaignId.item(0)));

	        	NodeList streetAddress = element.getElementsByTagName("streetAddress");
	        	temp.put("streetAddress", XMLUtils.getCharacterDataFromElement((Element) streetAddress.item(0)));
	        	
	        	NodeList streetAddress2 = element.getElementsByTagName("streetAddress2");
	        	temp.put("streetAddress2", XMLUtils.getCharacterDataFromElement((Element) streetAddress2.item(0)));

	        	NodeList cityName = element.getElementsByTagName("cityName");
	        	temp.put("cityName", XMLUtils.getCharacterDataFromElement((Element) cityName.item(0)));

	        	NodeList postalCode = element.getElementsByTagName("postalCode");
	        	temp.put("postalCode", XMLUtils.getCharacterDataFromElement((Element) postalCode.item(0)));

	        	NodeList countryCode = element.getElementsByTagName("countryCode");
	        	temp.put("countryCode", XMLUtils.getCharacterDataFromElement((Element) countryCode.item(0)));
	        	
	        	NodeList provinceName = element.getElementsByTagName("provinceName");
	        	temp.put("provinceName", XMLUtils.getCharacterDataFromElement((Element) provinceName.item(0)));

	        	NodeList phoneNumber = element.getElementsByTagName("phoneNumber");
	        	temp.put("phoneNumber", XMLUtils.getCharacterDataFromElement((Element) phoneNumber.item(0)));

	        	NodeList companyName = element.getElementsByTagName("companyName");
	        	temp.put("companyName", XMLUtils.getCharacterDataFromElement((Element) companyName.item(0)));

	        	NodeList status = element.getElementsByTagName("status");
	        	temp.put("status", XMLUtils.getCharacterDataFromElement((Element) status.item(0)));
	        	
	        	NodeList clicks = element.getElementsByTagName("clicks");
	        	temp.put("clicks", XMLUtils.getCharacterDataFromElement((Element) clicks.item(0)));

	        	NodeList cost = element.getElementsByTagName("cost");
	        	Element costElement = (Element) cost.item(0);
	        	NodeList costAmount = costElement.getElementsByTagName("microAmount");
	        	temp.put("cost", 
	        			Converter.microamountToStr(XMLUtils.getCharacterDataFromElement((Element) costAmount.item(0))
	        					));
	        	
	        	NodeList ctr = element.getElementsByTagName("ctr");
	        	temp.put("ctr", Converter.formatStrNumberPercent(
	        			XMLUtils.getCharacterDataFromElement((Element) ctr.item(0))
	        			));

	        	NodeList impressions = element.getElementsByTagName("impressions");
	        	temp.put("impressions", XMLUtils.getCharacterDataFromElement((Element) impressions.item(0)));
	        	
	        	Act_Super.uppendStatsToTotal(temp);
	        	
	        	list.add(temp);
	        }
	        
	        Act_Super.calculateTotalStats();
	        
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }

		return list;
	}
	
	
	
	/*
	 * Parse site links extension list
	 */
	public ArrayList<Map<String, String>> getSiteLinksExtensionList(){
		ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
		
		try {
	        
			Act_Super.resetTotals();
			
			NodeList nodes = _XMLDocument.getElementsByTagName("entries");
			
	        for (int i = 0; i < nodes.getLength(); i++) {
                //fill up hush map for list population
            	Map<String, String> temp = new HashMap<String, String>();

	        	Element element = (Element) nodes.item(i);

	        	NodeList adExtensionType = element.getElementsByTagName("AdExtension.Type");
	        	if(!XMLUtils.getCharacterDataFromElement((Element) adExtensionType.item(0)).equals("SitelinksExtension"))
	        		continue;
	        	
	        	NodeList id = element.getElementsByTagName("id");
	        	temp.put("id", XMLUtils.getCharacterDataFromElement((Element) id.item(0)));
	        	
	        	NodeList campaignId = element.getElementsByTagName("campaignId");
	        	temp.put("campaignId", XMLUtils.getCharacterDataFromElement((Element) campaignId.item(0)));
	        	
	        	
	        	NodeList sitelinks = element.getElementsByTagName("sitelinks");
	        	for (int j = 0; j < sitelinks.getLength(); j++) {
	        		Element sitelink = (Element) sitelinks.item(j);
		        	
	        		NodeList displayText = sitelink.getElementsByTagName("displayText");
		        	temp.put("displayText" + j, XMLUtils.getCharacterDataFromElement((Element) displayText.item(0)));

		        	NodeList destinationUrl = sitelink.getElementsByTagName("destinationUrl");
		        	temp.put("destinationUrl" + j, XMLUtils.getCharacterDataFromElement((Element) destinationUrl.item(0)));	        		
	        	}

	        	NodeList status = element.getElementsByTagName("status");
	        	temp.put("status", XMLUtils.getCharacterDataFromElement((Element) status.item(0)));
	        	
	        	NodeList clicks = element.getElementsByTagName("clicks");
	        	temp.put("clicks", XMLUtils.getCharacterDataFromElement((Element) clicks.item(0)));

	        	NodeList cost = element.getElementsByTagName("cost");
	        	Element costElement = (Element) cost.item(0);
	        	NodeList costAmount = costElement.getElementsByTagName("microAmount");
	        	temp.put("cost", 
	        			Converter.microamountToStr(XMLUtils.getCharacterDataFromElement((Element) costAmount.item(0))
	        					));
	        	
	        	NodeList ctr = element.getElementsByTagName("ctr");
	        	temp.put("ctr", Converter.formatStrNumberPercent(
	        			XMLUtils.getCharacterDataFromElement((Element) ctr.item(0))
	        			));

	        	NodeList impressions = element.getElementsByTagName("impressions");
	        	temp.put("impressions", XMLUtils.getCharacterDataFromElement((Element) impressions.item(0)));
	        	
	        	Act_Super.uppendStatsToTotal(temp);
	        	
	        	list.add(temp);
	        }
	        
	        Act_Super.calculateTotalStats();
	        
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }

		return list;
		
	}
}
